import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../view/Login/index.vue'),
  },
  {
    path: '/index',
    name: 'index',
    redirect: '/index/firstHome',
    component: () => import('../view/home/index.vue'),
    children: [
      {
        path: '/index/firstHome',
        name: 'firstHome',
        component: () => import('../view/firstHome/index.vue'),
      },
      {
        path: '/index/account',
        name: 'account',
        component: () => import('../view/account/index.vue'),
      },
      {
        path: '/index/commodity',
        name: 'commodity',
        component: () => import('../view/commodity/index.vue'),
      },
      {
        path: '/index/store',
        name: 'store',
        component: () => import('../view/store/index.vue'),
      },
      {
        path: '/index/supplier',
        name: 'supplier',
        component: () => import('../view/supplier/index.vue'),
      }
      ,
      {
        path: '/index/system',
        name: 'system',
        component: () => import('../view/system/index.vue'),
      },
      {
        path: '/index/purchase',
        name: 'purchase',
        component: () => import('../components/Purchase.vue'),

      },
      {
        path: '/index/warehouse',
        name: 'warehouse',
        component: () => import('../components/Warehouse.vue'),

      },
      {
        path: '/index/purchase/purchaseAdd',
        name: 'purchaseAdd',
        component: () => import('../components/AddPurchase.vue'),
      },
      {
        path: '/index/addwarehouse',
        name: 'addwarehouse',
        component: () => import('../components/AddWarehouse.vue'),

      },
      {
        path: '/index/statistic',
        name: 'statistic',
        component: () => import('../view/report/statistics.vue'),
      },
      {
        path: '/index/commodityRank',
        name: 'commodityRank',
        component: () => import('../view/report/commodityRank.vue'),
      },

    ],
  },
  {
    path: '/noPermission',
    name: 'noPermission',
    component: () => import('../view/403/index.vue'),
    meta: { title: '403' },
  },
  {
    path: '/:pathMatch(.*)',
    name: 'error',
    component: () => import('../view/404/index.vue'),
    meta: { title: '404' },
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes: routes
});

const isLogin = function () {
  let login = sessionStorage.getItem("access_token");
  return !!login;
};


router.beforeEach((to, from, next) => {
  let exPath = ['/home', '/noPermission']
  if (exPath.includes(to.path) || to.name === 'error') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('access_token')
  if (!tokenStr) return next('/noPermission')
  next()
})

export default router;